﻿using NetTaste;
using SimpleInOutControl.Common;
using SimpleInOutControl.DBModels;
using System.Drawing.Printing;

namespace SimpleInOutControl.Services
{
    public class UserInfoService: BaseService
    {
        public bool AddData(UserInfo dto)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var count = client.Insertable(dto).ExecuteCommand();
                return count > 0;
            }
        }
        public bool UpdateData(UserInfo dto)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var count = client.Updateable(dto).IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true).ExecuteCommand();
                return count > 0;
            }
        }

        public bool DeleteData(int id)
        {
            //系统管理员不允许删除
            if (id == 1)
            {
                return false;
            }
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var obj = client.Queryable<UserInfo>().Where(u => u.ID == id).First();
                if (obj == null)
                {
                    return false;
                }
                var count = client.Deleteable(obj).ExecuteCommand();
                return count > 0;
            }
        }

        public UserInfo GetData(int id)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var data = client.Queryable<UserInfo>().Where(u => u.ID == id).First();
                return data;
            }
        }

        public PagingData<UserInfo> GetPageData(PagingRequest<UserInfo> dto)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var datas = client.Queryable<UserInfo>().OrderBy(GetOrderString(dto.SortColumns, dto.IsAsc));
                //var list = datas.ToPageList(1, 3);
                //var pageIndex = 1;
                //var pageSize = 10;
                //var list = _client.Queryable<T>();
                //if (funcWhere != null)
                //{
                //    list = list.Where(funcWhere);
                //}
                //list = list.OrderByIF(true, funcOrderby);
                var result = new PagingData<UserInfo>()
                {
                    DataList = datas.ToPageList(dto.PageIndex, dto.PageSize),
                    PageIndex = dto.PageIndex,
                    PageSize = dto.PageSize,
                    RecordCount = datas.Count()
                };
                return result;
            }
        }

        public UserInfo Login(UserInfo dto)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var data = client.Queryable<UserInfo>().Where(u => u.Name == dto.Name && u.Password == dto.Password).First();
                if (data == null)
                {
                    return null;
                }
                data.LastLoginTime = DateTime.Now;
                client.Updateable(data).ExecuteCommand();
                return data;
            }
        }
    }
}
